package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGroupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGrouponRulesService;
import com.qf.service.DtsGrouponService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/12/1 16:10
 * description:团购规则
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/groupon")
public class AdminGrouponRulesController {
    @Autowired
    private DtsGrouponRulesService grouponRulesService;
    //团购列表
    @RequestMapping("list")
    public Object list( String goodsId,
                       @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(name = "order",required = false,defaultValue = "desc") String order
    ){
        IPage<DtsGrouponRules> grouponRules = grouponRulesService.findAll(goodsId,limit,page,sort,order);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",grouponRules.getTotal());
        data.put("items",grouponRules.getRecords());
        return ResponseUtil.ok(data);
    }
    @Autowired
    private DtsGoodsService goodsService;
    //团购添加
    @RequestMapping("create")
    public Object create(@RequestBody Map<String,Object> map)throws Exception{
        String goodsId = (String)map.get("goodsId");
        String discount = (String)map.get("discount");
        String discountMember = (String)map.get("discountMember");
        String expiretime = (String)map.get("expireTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        if(goodsId == null){
            return ResponseUtil.fail(-1,"商品ID不为空");
        }
        DtsGoods goods = goodsService.findByid(Integer.parseInt(goodsId));
        if(goods == null){
            return ResponseUtil.fail(-1,"商品不存在");
        }
        if(expiretime == null){
            return ResponseUtil.fail(-1,"结束时间不能为空");

        }
        Date date = new Date();
        if(sdf.parse(expiretime).getTime() < date.getTime()){
            return ResponseUtil.fail(-1,"团购结束时间错误");
        }


        DtsGrouponRules dtsGrouponRules = grouponRulesService.add(discount, discountMember, sdf.parse(expiretime), goods);


        return ResponseUtil.ok(dtsGrouponRules);
    }
    //团购修改
    @RequestMapping("update")
    public Object update(@RequestBody DtsGrouponRules grouponRules){

        if(grouponRules == null ){
            return ResponseUtil.badArgument();
        }

        if(grouponRules.getGoodsId() == null){
            return ResponseUtil.fail(-1,"商品ID不为空");
        }

        DtsGoods goods = goodsService.findByid(grouponRules.getGoodsId().intValue());
        if(goods == null){
            return ResponseUtil.fail(-1,"商品不存在");
        }
        if(grouponRules.getExpireTime() == null){
            return ResponseUtil.fail(-1,"结束时间不能为空");

        }
        Date date = new Date();


        if(grouponRules.getExpireTime().getTime() < date.getTime()){
            return ResponseUtil.fail(-1,"团购结束时间错误");
        }
        return grouponRulesService.save(grouponRules, goods);
    }
    //团购删除
    @RequestMapping("delete")
    public Object delete(@RequestBody DtsGrouponRules grouponRules){
        if(grouponRules == null){
            return ResponseUtil.badArgument();
        }
        return grouponRulesService.delete(grouponRules);
    }
    @Autowired
    private DtsGrouponService grouponService;
    //团购活动
    @RequestMapping("listRecord")
    public Object listRecord( String rulesId,
                              @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                              @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
                              @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
                              @RequestParam(name = "order",required = false,defaultValue = "desc") String order
    ){
        IPage<DtsGroupon> grouponRules = grouponService.findAll(rulesId,limit,page,sort,order);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",grouponRules.getTotal());
        data.put("items",grouponRules.getRecords());
        return ResponseUtil.ok(data);
    }
}
